#include "OtherEquipmentXml.h"
#include <QObject>
#include "Command/PointProtocol/UdpPointSetSwitchCmd.h"
#include "DataType/PointSwitchUdpStruct.h"
#include "Port/UDP.h"
#include "System/System.h"
OtherEquipmentXml::OtherEquipmentXml()
{
}
bool OtherEquipmentXml::clear()
{
    return false;
}
bool OtherEquipmentXml::parser()
{
    if(!this->check())
    {
      //  return ;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();
    StructList structList;
    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();
    QList<PointSwitchUdpStruct> pointSwitchUdpList;
    while(!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "SmartSocket")
        {
            PointSwitchUdpStruct pointData;
            QDomNode portListChild=rootChild.firstChild();
            while(!portListChild.isNull())
            {
//                if(portListChild.toElement().tagName() == "DeviceID")
//                {
//                    QString getDeviceID = portListChild.toElement().text();
//                    pointData.SetName(getDeviceID);
//            //        qDebug()<<"getDevice ID="<<getDeviceID;
//                }
                if(portListChild.toElement().tagName() == "ID")
                                {
                                    QString getDeviceID = portListChild.toElement().text().remove(" ");
                                    pointData.SetName(getDeviceID);
                                    qDebug()<<"getDevice ID="<<getDeviceID;
                                }
                if(portListChild.toElement().tagName() == "UDPIP")
                {
                    QString getIP = portListChild.toElement().text();
                    pointData.SetIP(getIP);
             //       qDebug()<<"IP =="<<getIP;
                }
                if(portListChild.toElement().tagName() == "addr")
                {
                    QString getAddr = portListChild.toElement().text();
                    quint16 m_getAddr=getAddr.toInt();
                    pointData.SetAddr(m_getAddr);
              //      qDebug()<<"SetAddr="<<m_getAddr;
                }
                portListChild=portListChild.nextSibling();
            }
            pointSwitchUdpList.append(pointData);
         }
            rootChild=rootChild.nextSibling();

    }
    m_pointSwitchUdpList=pointSwitchUdpList;
  //  qDebug()<<"List now";
     //foreach (const PointSwitchUdpStruct &pointData, pointSwitchUdpList)
  //   {
    //     qDebug()<<"getDevice ID="<<pointData.Name();
    //     qDebug()<<"SetAddr="<<pointData.Addr();
    //     qDebug()<<"IP="<<pointData.IP();
 //    }
    return true;
}
bool OtherEquipmentXml::modify(PortStruct target,QSharedPointer<BaseCmd> sharedCmd)
{
    UdpPointSetSwitchCmd *UdpData =qobject_cast<UdpPointSetSwitchCmd*>(sharedCmd.data());

    quint16 addr    =UdpData->address();
    quint16 deviceID =UdpData->DeviceID();
    quint16 portnum      =target.port();
    QByteArray value   =UdpData->value();
    QString IP      =target.ip();
    QString data_Addr = QString::number(addr);
    QString data_DeviceID = QString::number(deviceID);
    QString data_portnum = QString::number(portnum);
 //   QString data_ChipID = QString::number(Value);
 //   OtherEquipmentXml::setPath("/Xml/OtherEquipment.xml");
    if(!this->check())
    {
        return false;
    }

//    m_OtherEquipmentList.append(OtherEquipment);
    qDebug()<<"open now.....1";

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();
    qDebug()<<"open now.....2";
    QDomElement root=doc.documentElement();
    QDomText domText;
 //   if(root.tagName()!="EquipmentElement")
//    {
//        QDomElement root = doc.createElement("EquipmentElement");
        doc.appendChild(root);
//    }
    QDomNode node=root.firstChild(); //获得第一个子节点
    while(!node.isNull())
    {
        QDomNode nodechild = node.firstChild();
        while(!nodechild.isNull())
        {
           if(nodechild.toElement().tagName() == "ID")
           {
            QString xml_ChipID= nodechild.toElement().text();
           if(xml_ChipID==value)
            {
                //检测到相同芯片ID
                qDebug()<<"Found same a data";
                while(!nodechild.isNull())
                {
                    if(nodechild.toElement().tagName() == "DeviceID")
                    {
                        QString xml_DeviceID= nodechild.toElement().text();
                        if(xml_DeviceID!=data_DeviceID)
                        {
                            qDebug()<<"But turn DeviceID";
                            QDomNode oldDeviceID=nodechild;
                            oldDeviceID.firstChild().setNodeValue(data_DeviceID);
                            QDomNode newDeviceID=oldDeviceID.firstChild();
                            oldDeviceID.replaceChild(newDeviceID,oldDeviceID);
                        }
                    }
                    if(nodechild.toElement().tagName() == "UDPIP")
                    {
                        QString xml_UDPIP= nodechild.toElement().text();
                        if(xml_UDPIP!=IP)
                        {
                            qDebug()<<"But turn IP";
                            QDomNode oldUDPIP=nodechild;
                            oldUDPIP.firstChild().setNodeValue(IP);
                            QDomNode newUDPIP=oldUDPIP.firstChild();
                            oldUDPIP.replaceChild(newUDPIP,oldUDPIP);
                        }
                    }
                    if(nodechild.toElement().tagName() == "addr")
                    {
                        QString xml_Addr= nodechild.toElement().text();
                        if(xml_Addr!=data_Addr)
                        {
                            qDebug()<<"But turn addr";
                            QDomNode oldAddr=nodechild;
                            oldAddr.firstChild().setNodeValue(data_Addr);
                            QDomNode newAddr=oldAddr.firstChild();
                            oldAddr.replaceChild(newAddr,oldAddr);
                        }
                    }
                    nodechild=nodechild.nextSibling();
                }
                if(!file.open(QFile::WriteOnly|QFile::Truncate))
                        return false;
                    //输出到文件
                QTextStream out_stream(&file);
                doc.save(out_stream,4); //缩进4格
                file.close();

                System *system = System::getInstance();
                QString appDirPath = system->appDirPath();
                QString rootPath = system->rootPath();
                OtherEquipmentXml *otherEquipmentXml = OtherEquipmentXml::getInstance();
                otherEquipmentXml->setPath(appDirPath + "/Xml/OtherEquipment.xml");
                otherEquipmentXml->parser();
                return true;
            }
           }
         nodechild=nodechild.nextSibling();
       }
         node=node.nextSibling();
   }
    //遍历无相同芯片ID，新建
    qDebug()<<"take new xml.....";
    OtherEquipmentXml::NewXmldata(target,sharedCmd);
    file.close();
    System *system = System::getInstance();
    QString appDirPath = system->appDirPath();
    QString rootPath = system->rootPath();
    OtherEquipmentXml *otherEquipmentXml = OtherEquipmentXml::getInstance();
    otherEquipmentXml->setPath(appDirPath + "/Xml/OtherEquipment.xml");
    otherEquipmentXml->parser();
    return true;
}

bool OtherEquipmentXml::NewXmldata(PortStruct target,QSharedPointer<BaseCmd> sharedCmd)
{
    UdpPointSetSwitchCmd *UdpData =qobject_cast<UdpPointSetSwitchCmd*>(sharedCmd.data());

    quint16 addr    =UdpData->address();
    quint16 deviceID =UdpData->DeviceID();
    quint16 portnum      =target.port();
    QByteArray value   =UdpData->value();
    QString IP      =target.ip();    
    QString data_Addr = QString::number(addr);
    QString data_DeviceID = QString::number(deviceID);
    QString data_portnum = QString::number(portnum);
    qDebug()<<"IP"<<IP<<"---------IP"<<addr;
    qDebug()<<"portnum"<<portnum;
    qDebug()<<"ADDR"<<data_Addr<<"-------Addr"<<value;
    qDebug()<<"CHIPID"<<value;
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();


    QDomElement write_Type=doc.createElement("SmartSocket");
    QDomText IDText;
    QDomElement Write_ID=doc.createElement("ID");
    IDText = doc.createTextNode(value);
    Write_ID.appendChild(IDText);
    write_Type.appendChild(Write_ID);

    QDomText deviceIDText;
    QDomElement Write_deviceID=doc.createElement("DeviceID");
    deviceIDText = doc.createTextNode(data_DeviceID);
    Write_deviceID.appendChild(deviceIDText);
    write_Type.appendChild(Write_deviceID);

    QDomText IPText;
    QDomElement Write_IP=doc.createElement("UDPIP");
    IPText = doc.createTextNode(IP);
    Write_IP.appendChild(IPText);
    write_Type.appendChild(Write_IP);

    QDomText AddrText;
    QDomElement Write_Addr=doc.createElement("addr");
    AddrText = doc.createTextNode(data_Addr);
    Write_Addr.appendChild(AddrText);
    write_Type.appendChild(Write_Addr);

    root.appendChild(write_Type);

    doc.appendChild(root);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;

}


